<template>
  <el-dialog
  :title="title"
  :visible.sync="visible"
  width="40%"
  :before-close="handleClose">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" class="demo-ruleForm" style="width:90%">
    <el-form-item label="用户名：" prop="username">
        <el-input v-model="formData.username"></el-input>
    </el-form-item>
    <el-form-item label="昵称：" prop="nickName">
        <el-input v-model="formData.nickName"></el-input>
    </el-form-item>
    <el-form-item label="手机号：" prop="mobile">
        <el-input v-model="formData.mobile"></el-input>
    </el-form-item>
    <el-form-item label="邮箱：" prop="email">
        <el-input v-model="formData.email"></el-input>
    </el-form-item>
    <el-form-item label="帐号过期：" prop="isAccountNonExpired">
        <el-radio-group v-model="formData.isAccountNonExpired">
        <el-radio :label="1"  border>未过期</el-radio>
        <el-radio :label="0" border>已过期</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="密码过期：
" prop="isCredentialsNonExpired">
        <el-radio-group v-model="formData.isCredentialsNonExpired">
        <el-radio :label="1"  border>未过期</el-radio>
        <el-radio :label="0" border>已过期</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="帐号锁定：" prop="isAccountNonLocked">
        <el-radio-group v-model="formData.isAccountNonLocked">
        <el-radio :label="1"  border>未锁定</el-radio>
        <el-radio :label="0" border>已锁定</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" size="mini" @click="submitForm('formData')">确定</el-button>
        <el-button @click="handleClose" size="mini">取消</el-button>
    </el-form-item>
    </el-form>
    </el-dialog>
</template>

<script>
import * as api from '@/api/user';    
export default {
    data() {
        return {
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                    
                ],
                nickName: [
                    { required: true, message: '请输入昵称', trigger: 'blur' }
                    
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' }
                    
                ],
                isAccountNonExpired: [
                    { required: true, message: '请选择', trigger: 'change' },                   
                ],
                isAccountNonLocked: [
                    { required: true, message: '请选择', trigger: 'change' },                   
                ],
                isCredentialsNonExpired: [
                    { required: true, message: '请选择', trigger: 'change' },                   
                ],
            }
        }
    },
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        title:{
            type:String,
            default:''
        },
        formData:{
            type:Object,
            default:{}
        },
        remoteClose: Function 
    },
    methods: {
        // 关闭窗口
        handleClose(){
            this.$refs.formData.resetFields()
            this.remoteClose()
        },
        // 提交表格，验证
        submitForm(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submitData()
                } else {
                    this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
                    return false;
                }
            });
        },
        // 提交数据，检验
        async submitData(){
            let response=null
            if(this.formData.id){
                response = await api.update(this.formData)

            }else{
                response = await api.add(this.formData)
            }
            
            if (response.code === 20000){
                this.$message({
                    message: '保存成功',
                    type: 'success'
                    });
                this.handleClose()
                
            }else{
                this.$message({
                    message: '保存失败',
                    type: 'error'
                    });
            }
        }
    },
}
</script>

<style>

</style>